/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.xb.ctx.attr.factory;

import axescon.xacml.api.ExpressionEvalIndeterminateException;
import axescon.xacml.model.ctx.AttributeValue;
import axescon.xacml.model.shared.attr.DnsNameAttribute;
import axescon.xacml.xb.ctx.XBAttributeValueFactory;
import org.apache.xmlbeans.XmlCursor;
import x0ContextSchemaOs.oasisNamesTcXacml2.AttributeValueType;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: argyn
 * Date: Jun 18, 2005
 * Time: 2:19:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class DnsNameXBAttrValueFactory implements XBAttributeValueFactory {


    public DnsNameAttribute getDnsNameAttributeValue(AttributeValueType vt) {
        XmlCursor xmlCursor = vt.newCursor();
        xmlCursor.toNextToken();
        return new DnsNameAttribute(xmlCursor.getChars());

    }

    public AttributeValue getAttributeValue(AttributeValueType vt) {
        return getDnsNameAttributeValue(vt);
    }

    public axescon.xacml.model.policy.AttributeValue getAttributeValue(x0PolicySchemaOs.oasisNamesTcXacml2.AttributeValueType vt) {
        return getDnsNameAttribute(vt);
    }


    public DnsNameAttribute getDnsNameAttribute(x0PolicySchemaOs.oasisNamesTcXacml2.AttributeValueType vt) {
        XmlCursor xmlCursor = vt.newCursor();
        xmlCursor.toFirstContentToken();
        DnsNameAttribute ret = new DnsNameAttribute(xmlCursor.getChars());
        xmlCursor.dispose();
        return ret;
    }

    /**
     * get values found by selectPath on cursor
     *
     * @param cur
     * @return
     */
    public axescon.xacml.model.policy.AttributeValue[] getAttributeValues(XmlCursor cur) throws ExpressionEvalIndeterminateException {
        try {
            if (!cur.hasNextSelection()) return null;
            ArrayList ret = new ArrayList(1);
            while (cur.hasNextSelection()) {
                cur.toNextSelection();
                ret.add(new DnsNameAttribute(cur.getTextValue()));
            }
            return (axescon.xacml.model.policy.AttributeValue[]) ret.toArray(new DnsNameAttribute[ret.size()]);
        } catch (RuntimeException e) {

            throw new ExpressionEvalIndeterminateException(e);
        }

    }

}
